package com.hos.db.create.ill;

import com.hos.db.entity.ill.IllResultEntity;
import com.hos.utile.DLog;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;

import java.util.Date;
import java.util.List;

/**
 * 病情治疗结果/出院情况
 * Created by guom on 2018/10/15.
 */
public class IllResultTable {
    public static String tableName = "pat_ill_results";
    public static String tableIllId = "ill_id";//病例id
    public static String tableCureResult = "cure_result";//治疗结果
    public static String tableHospitalOutSuggest = "hospital_out_suggest";//出院建议
    public static String tableResultExplain = "result_explain";//补充说明
    public static String tableCreateTime = "create_time";// 创建时间
    //
    public static String careteTable = "create table  "
            + tableName + " ( "
            + tableIllId + "  varchar(200), "
            + tableCureResult + " longText, "
            + tableHospitalOutSuggest + " longText, "
            + tableResultExplain + " longText, "
            + tableCreateTime + " datetime "
            + "); ";

    public static boolean onInsert(JdbcTemplate jdbcTemplate, String illId, IllResultEntity bean) {
        if (bean == null) {
            bean = new IllResultEntity();
        }
        String sql = "INSERT INTO " + IllResultTable.tableName
                + "("
                + IllResultTable.tableIllId + ","
                + IllResultTable.tableCureResult + ","
                + IllResultTable.tableHospitalOutSuggest + ","
                + IllResultTable.tableResultExplain + ","
                + IllResultTable.tableCreateTime
                + ")"
                + " VALUES (?,?,?, ?,? )";

        Object[] obj = new Object[]{illId, bean.getCureResult(), bean.getHospitalOutSuggest(),
                bean.getResultExplain(), new Date()};
        jdbcTemplate.update(sql, obj);
        return true;
    }

    //删除
    public static boolean onDlete(JdbcTemplate jdbcTemplate, String illId) {
        String sql = "DELETE  FROM " + IllResultTable.tableName
                + " WHERE "
                + IllResultTable.tableIllId + " = '" + illId + "'";
        int state = jdbcTemplate.update(sql);
        return state > 0;
    }

    //修改
    public static boolean onUpdate(JdbcTemplate jdbcTemplate, String illId, IllResultEntity bean) {
        if (bean == null) {
            return true;
        }
        String update = "";
        String value = bean.getCureResult();
        if (value != null) {
            update += " , " + IllResultTable.tableCureResult + " = '" + value + "'";
        }
        value = bean.getHospitalOutSuggest();
        if (value != null) {
            update += " , " + IllResultTable.tableHospitalOutSuggest + " = '" + value + "'";
        }
        value = bean.getResultExplain();
        if (value != null) {
            update += " , " + IllResultTable.tableResultExplain + " = '" + value + "'";
        }
        if (update.equals("")) {
            return true;
        }
        update = update.replaceFirst(" , ", "");
        String sql = "UPDATE " + IllResultTable.tableName
                + " SET "
                + update
                + " WHERE "
                + IllResultTable.tableIllId + " = '" + illId + "'";
        int s = jdbcTemplate.update(sql);
        return s > 0;
    }

    public static IllResultEntity getResults(JdbcTemplate jdbcTemplate, String illId) {
        String sql = "SELECT * FROM " + IllResultTable.tableName
                + " WHERE "
                + IllResultTable.tableIllId + " = '" + illId + "'";
        IllResultEntity entity = new IllResultEntity();
        List<IllResultEntity> ills = onQuery(jdbcTemplate, sql);
        if (ills != null && ills.size() > 0) {
            entity = ills.get(0);
        }
        return entity;
    }


    private static List<IllResultEntity> onQuery(JdbcTemplate jdbcTemplate, String sql) {
        List<IllResultEntity> ills = null;
        try {
            ills = jdbcTemplate.query(sql,
                    ParameterizedBeanPropertyRowMapper.newInstance(IllResultEntity.class));
            DLog.e("查询成功：==========");
        } catch (Exception e) {
            DLog.e("查询失败：" + e.getMessage());
        }
        return ills;
    }

}
